package com.hzcx.app.simplechat.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

import androidx.appcompat.widget.AppCompatButton;

import com.hzcx.app.simplechat.R;

/**
 *  所有统一的蓝色按钮
 */
public class MainBlueConfirmButton extends AppCompatButton {

    private Context context;

    public MainBlueConfirmButton(Context context) {
        super(context);
        this.context=context;
        init();
    }

    public MainBlueConfirmButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        init();
    }

    public MainBlueConfirmButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        init();
    }

    /**
     *  初始化
     */
    private void init(){
        setBtnNormal();
    }

    /**
     *  设置按钮可点击
     */
    private void setBtnSelete(){
        setEnabled(true);
        setBackgroundResource(R.drawable.btn_main_blue_confirm_bg_selete);
    }

    /**
     *  设置按钮未选中
     */
    private void setBtnNormal(){
        setBackgroundResource(R.drawable.btn_main_blue_confirm_bg_normal);
        setEnabled(false);
    }

    /**
     *  按钮是否可点击
     * @param isEnabled
     */
    public void setBtnEnabled(boolean isEnabled){
        if (isEnabled){
            setBtnSelete();
        }else {
            setBtnNormal();
        }
    }

    /**
     *  设置监听的输入框
     * @param editText
     */
    public void setEditText(EditText editText,int minLength){
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(editText.getText().toString().length()>=minLength){
                    setBtnEnabled(true);
                }else {
                    setBtnEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }
}
